package email;

import java.awt.Component;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 * Exports the contents of emails and threads to a file.
 * @author Jamison
 *
 */
public class Exporter {
	/**
	 * the component that used for displaying messages to user
	 */
	Component display;
	
	/**
	 * The file that the email or thread should be exported to
	 */
	File output;
	
	public Exporter(Component forDisplay){
		//the root pane that will be used for displaying info
		display = forDisplay;
	}
	
	/**
	 * exports given string to choosen file
	 * @param s the string to be exported
	 */
	public void export(String s){
		//if the user doesn't select a file exit method	
		boolean choice = selectFile();
		if(!choice) return;
		
		//call helper method to write the text to the output file
		boolean success = writeToFile(s);
		
		//inform the user of the outcome of their attempted export
		if (success){
			JOptionPane.showMessageDialog(display, "The file was exported successfully!");
		}
		else{
			JOptionPane.showMessageDialog(display, "The export has failed!");
		}
	}
	
	/**
	 * Exports the contents of an email to a text file.
	 * @param m the message to be exported
	 */
	public void exportEmail(ReceivedMessage m){
		//if the user doesn't select a file exit method	
		boolean choice = selectFile();
		if(!choice) return;
			
		//grab the info about the email that will be exported with the email
		String toExport = "From: " + m.getFrom() + "\n";
		toExport += "Subject: " + m.getSubject() + "\n";
		toExport += "Date: " + m.getDate() + "\n\n";
		toExport += m.getBody();
		//call helper method to write the text to the output file
		boolean success = writeToFile(toExport);
		
		//inform the user of the outcome of their attempted export
		if (success){
			JOptionPane.showMessageDialog(display, "The file was exported successfully!");
		}
		else{
			JOptionPane.showMessageDialog(display, "The export has failed!");
		}
	}
	
	/**
	 * Prompts user to select or enter a file
	 * @return true if they choose a file false if they don't choose one
	 */
	private boolean selectFile(){
		//allow user to select the file that the message should be exported to
		JFileChooser choice = new JFileChooser();
		int value = choice.showSaveDialog(display);
		
		//if user selected or entered file export the email
		if (value == JFileChooser.APPROVE_OPTION){
			//grab file name and test if extension is provided
			File testFile = choice.getSelectedFile();
			String testString = testFile.getName();

			//test to see if an extension was provided and if so set output
			for (int i = 0; i < testString.length(); i++){
				if (testString.charAt(i) == '.'){
					output = choice.getSelectedFile();
					return true;
				}
			}
			
			//no extension was provided so make it a .txt
			output = new File(choice.getSelectedFile() + ".txt");
			return true;
		}
		//no file entered or selected return false
		else return false;
	}
	
	/**
	 * Writes a given string to the file global variable
	 * @param s the string to be written
	 * @return true if written correctly false if not
	 */
	private boolean writeToFile(String s){
		//Code provided in homework 1
		try{
			PrintWriter out = new PrintWriter(new FileWriter(output));
			// "contents" hold the text in your text area in the form of String
			out.print(s);
			if (out.checkError())
				throw new IOException("Error while writing to file.");
			out.close();
		} 
		catch (IOException e1) {
			e1.printStackTrace();
			return false;
		}
		return true;
	}

}
